package org.nokatag.tagjava;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.password.AESPlus;
import org.nokatag.system.Concest;

public class InputText extends Inputsuper{

	private static final long serialVersionUID = 8790633944771551136L;
	private String veri = null;//验证
	private String chtype = null;//验证类型 chtype js|server|regex|ext
	private String mesg = "erro mesages for input!";
	private String maxclass = null;//放大效果的class
	private String onshowmax = null;
	private String allownull="true";
	private String dfmt = null;//格式化日期 
	public int doStartTag()throws JspException{
		JspWriter jspOut = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		String basePath = basepath(request);
		String ver = veri;//org.noka.Ftest.rut(this.value,’a’,56,:id_java.lang.String,request)
		String pwidth = getWidth();
		pwidth =LenFormat((pwidth==null)?"120":pwidth);
		StringBuffer fsb = new StringBuffer();
		fsb.append("[");
		String funp=ver;
		FormHtmlInsertid(getId(),getName(),getValue());
		if("server".equals(chtype) && ver!=null && ver.trim().length()>0){
			ver ="verification:'"+ AESPlus.encrypt(ver, Concest.SQLKEY)+"'";//加密
			//-------------对调用页面其它控件件的值进行处理----------------------------
			funp=delEn(funp);//去掉换行符
			String snp = funp.substring(funp.indexOf("(")+1,funp.indexOf(")"));
			for(String p:snp.split(",")){//a,'b'
				p = p.trim();
				if(p.length()>0){//有参数
					if(p.startsWith(":") && p.indexOf("_")!=-1){//request value
						String vn[]=p.split("_");
						vn[0]=vn[0].substring(1);
						fsb.append("'"+vn[0]+"',");
					}
				}
			}
			if(fsb.length()>1){
				fsb.delete(fsb.length()-1,fsb.length()); 
			}
			//--------------------------------------------------------------------
		}else if("js".equals(chtype) && ver!=null && ver.trim().length()>0){
			ver ="verification:"+ ver;
		}else if("ext".equals(chtype) && ver!=null && ver.trim().length()>0){
			ver ="verification:"+ ver;
		}else if("regex".equals(chtype) && ver!=null && ver.trim().length()>0){
			ver ="verification:'"+ver+"'";
		}else{
			ver ="verification:''";
		}
		fsb.append("]");
		NOOUT.put("width", true);
		Integer swidth = Integer.parseInt(pwidth.substring(0,pwidth.length()-"px".length()))-0;
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(potjs(request));//加载Prototype
		stringBuilder.append(inputTextJS(request));//加载Prototype
		stringBuilder.append("<div style=\"float: left;\">");
		stringBuilder.append("<div id=\""+getId()+"_maxDiv\" "+(maxclass==null?"":"class=\""+maxclass+"\"")+" style=\"position:absolute;-moz-border-radius:5px; border-radius:5px;\"></div>");//放大效果的div
		stringBuilder.append("<div id=\""+getId()+"_sdDiv\" class=\"nk_input_lineheight nk_input_sdDiv\" style=\"width:"+pwidth+";\">");
		stringBuilder.append("<input type=\"text\" class=\"nk_input_lineheight nk_input_text\" style=\"width:"+swidth+"px\" ");
		stringBuilder.append(getBaseStr()+"/></div>");
		if("false".equalsIgnoreCase(allownull)){
			stringBuilder.append("<div style=\"color: red;float: left;\" class=\"nk_input_lineheight\">*</div>");
		}
		stringBuilder.append("<img id=\""+getId()+"_img\" style=\"float: left;\" class=\"nk_input_imgheight\"/><div class=\"nk_input_message\" nowrap id=\""+getId()+"_div\" style=\"position:absolute;display:none;\">"+mesg+"</div>");
		stringBuilder.append("</div>");
		stringBuilder.append("<script type=\"text/javascript\">");
		stringBuilder.append("Event.observe(document, 'dom:loaded', function() {");
		stringBuilder.append("var cfgs"+getId()+" = {id:'"+getId()+"',chtype:'"+(chtype==null?"":chtype)+"',"+ver+",allownull:"+allownull+",pva:"+fsb+",rurl:'"+basePath+"'");
		if(null!=maxclass){
			stringBuilder.append(",maxclass:'"+maxclass+"'");
		}
		if(null!=onshowmax){
			stringBuilder.append(",onShowMax:function(a){return "+onshowmax+"(a);}");
		}
		if(null!=dfmt){
			stringBuilder.append(",dfmt:'"+dfmt+"'");
		}
		stringBuilder.append("};");
		stringBuilder.append("var np"+getId()+" = new ninput(cfgs"+getId()+");");
		stringBuilder.append("np"+getId()+".initinput();");
		stringBuilder.append("});");
		stringBuilder.append("</script>");
		
		try {
			jspOut.write(stringBuilder.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	/**
	 * 去掉换行符
	 * @param str
	 * @return
	 */
	public static String delEn(String str){
		//-----------------去掉换行符--------------------------------------
		Pattern p = Pattern.compile("\\s*\n");
		Matcher m = p.matcher(str);
		str=m.replaceAll("").trim();
		return str;
		//----------------------------------------------------------------
	}
	public String getVeri() {
		return veri;
	}
	public void setVeri(String veri) {
		this.veri = veri;
	}
	public String getChtype() {
		return chtype;
	}
	public void setChtype(String chtype) {
		this.chtype = chtype;
	}
	public String getMesg() {
		return mesg;
	}
	public void setMesg(String mesg) {
		this.mesg = mesg;
	}
	public String getAllownull() {
		return allownull;
	}
	public void setAllownull(String allownull) {
		this.allownull = allownull;
	}
	public String getMaxclass() {
		return maxclass;
	}
	public void setMaxclass(String maxclass) {
		this.maxclass = maxclass;
	}
	public String getOnshowmax() {
		return onshowmax;
	}
	public void setOnshowmax(String onshowmax) {
		this.onshowmax = onshowmax;
	}
	public String getDfmt() {
		return dfmt;
	}
	public void setDfmt(String dfmt) {
		this.dfmt = dfmt;
	}
	
	
}
